﻿
layui.use(['tree','form','comment'], function () {
    var comment = layui.comment
        , $ = layui.$
        , table = layui.table
        , form = layui.form
        , tree = layui.tree;
    //渲染
    comment.Table_Rander({
        elem: '#role'
        , url: ApiUrl.LanbtRole.Search.url //数据接口
        , cols: [[ //表头
            { type: 'checkbox', fixed: 'left' },
            , { field: 'id', title: 'ID', hide: true }
            , { field: 'name', title: '角色名', edit: 'text' }
            , { field: 'createTime', title: '创建时间', templet: '<div>{{layui.util.toDateString(d.createTime,\'yyyy-MM-dd HH:mm\')}}</div>' }
            , { field: 'modificaTime', title: '修改时间', templet: '<div>{{layui.util.toDateString(d.modificaTime,\'yyyy-MM-dd HH:mm\')}}</div>' }
            , { fixed: 'right', minWidth: 200, title: '操作', align: 'center', toolbar: '#line_tool' }
        ]]
        , toolbar: '#heard_tool'
        , done: function (res, curr, count) {
            comment.CheckPermission()
        }
    });

    //头部工具栏事件
    table.on('toolbar(role)', function (obj) {
        var checkStatus = table.checkStatus(obj.config.id);
        switch (obj.event) {
            case 'search':
                var name = $("#name").val();
                table.reload("role", {
                    where: {
                        name: name
                    }
                    , done: function () {
                        $('#name').val(name);
                        comment.CheckPermission();
                    }
                    , page: {
                        pageIndex: 1
                    }
                })
                break;
            case 'add':
                layer.open({
                    type: 1
                    , resize: false
                    , title: '添加角色'
                    , content: $('#addrole-template').html()
                    , shade: 0.8//遮幕
                    , area: ['350px', '190px']//定义宽高
                    , success: function (layero, index) {
                        //这里因为内容过少, 会被遮挡, 所以简单修改了下样式
                        layero.find('.layui-layer-content').css('overflow', 'visible');

                        form.on('submit(save)', function (data) {

                            comment._ACP(ApiUrl.LanbtRole.Insert, JSON.stringify(data.field.name), function (res) {
                                if (res.success) {
                                    comment.Success("成功")
                                    setTimeout(function () {
                                        table.reload('role');
                                        layer.close(index);
                                    }, 200)
                                } else {
                                    comment.Error(res.errorMsg);
                                }
                            })

                        })
                    }
                })
                break;
            case 'batchDel':
                var checkStatus = table.checkStatus('role');
                var count = checkStatus.data.length;
                if (count == 0) {
                    layer.msg('至少选择一条数据');
                }
                else {
                    layer.confirm('确定删除' + count + '条用户吗？', function (index) {

                        let ids = [];
                        checkStatus.data.forEach(function (item) {
                            ids.push(item.id);
                        })
                        //do something
                        comment._ACP(ApiUrl.LanbtRole.DeleteManyByIds, JSON.stringify(ids), function (res) {
                            if (res.success) {
                                comment.Success(res.result)
                                table.reload("role");
                            } else {
                                comment.Error(res.errorMsg)
                            }
                        })
                    });
                }
                break;
        };
    });

    //行修改
    table.on('edit(role)', function (obj) {
        var data = obj.data;
        var update = {
            id: data.id,
            name: data.name
        };
        comment._ACP(ApiUrl.LanbtRole.Update, JSON.stringify(update), function (res) {
            if (res.success) {
                comment.Success(res.result)
            } else {
                comment.Error(res.errorMsg)
            }
        })
    })

    //监听行工具事件
    table.on('tool(role)', function (obj) { //注：tool 是工具条事件名，role 是 table 原始容器的属性 lay-filter="对应的值"
        var data = obj.data //获得当前行数据
            , layEvent = obj.event; //获得 lay-event 对应的值
        switch (layEvent) {
            case 'delete':
                layer.confirm('确定要删除用户【' + data.name + '】吗？', function (index) {
                    comment._ACP(ApiUrl.LanbtRole.DeleteOneById, JSON.stringify(data.id), function (res) {
                        if (res.success) {
                            comment.Success(res.result);
                            table.reload("role");
                        } else {
                            comment.Error(res.errorMsg)
                        }
                    })
                })
                break;
            case 'assign_power':
                layer.open({
                    type: 1
                    , resize: false
                    , title: '分配权限【' + data.name + '】'
                    , content: $('#assign-role-template').html()
                    , shade: 0.8//遮幕
                    , area: ['460px', '600px']//定义宽高
                    , btn:['确认']
                    , success: function (layero, index) {
                        comment._ACP(ApiUrl.LanbtRole.PowerTreeData, { id: data.id }, function (res) {
                            if (res.success) {
                                tree.render({
                                    elem: '#powerTree'  //绑定元素
                                    , id: 'powerTree'
                                    , showCheckbox: true
                                    , data: res.result
                                });
                            } else {
                                comment.Error(res.errorMsg)
                            }
                        })
                    }
                    , yes: function (index, layero) {
                        var checkData = tree.getChecked('powerTree');

                        comment._ACP(ApiUrl.LanbtRole.AssignPower, JSON.stringify({ id: data.id, datas: checkData }), function (res) {
                            if (res.success) {
                                comment.Success(res.result)
                                setTimeout(function () {
                                    table.reload('role');
                                    layer.close(index);
                                }, 200)
                            } else {
                                comment.Error(res.errorMsg);
                            }
                        })
                    }
                })
                break;
        }
    });
})